﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagement.Common;
using HotelManagement.BL;

namespace HotelManagement.PL
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
            UpdateGridView();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            UpdateGridView();
        }

        internal void UpdateGridView() 
        {
            int nResult;
            try
            {
                Goods goods = new Goods();
                goods.Name = txtName.Text.Trim();

                GoodsBL searchBL = new GoodsBL();
                nResult = searchBL.Search(ref goods);

                if (goods.GoodsCollection != null)
                {
                    dgvGoods.AutoGenerateColumns = false;
                    dgvGoods.DataSource = goods.GoodsCollection;
                }
            }
            catch 
            {
            
            }
        }

        private void btnPurchase_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0) 
            {
                return;
            }
            PurchaseForm purchaseForm = new PurchaseForm(TradeType.Purchase);
            purchaseForm.GoodInfo = buildGoods(nSelectRow);
            purchaseForm.ShowDialog(this);
        }

        private int CheckChoose() 
        {
            int nCount = 0;
            int nRowIndex = -1;
            foreach (DataGridViewRow dgr in dgvGoods.Rows) 
            {
                if (dgr.Cells["choose"].Value != null && dgr.Cells["choose"].Value.Equals(true)) 
                {
                    nCount++;
                    nRowIndex = dgr.Index;
                }
            }

            if (nCount == 0) 
            {
                MessageBox.Show("请选择货物", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                nRowIndex = -1;
            }

            if (nCount > 1)
            {
                MessageBox.Show("只能选择一个", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                nRowIndex = -1;
            }

            return nRowIndex;
        }

        private Goods buildGoods(int rowIndex)
        {
            Goods goods = new Goods();
            goods.ID = Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["ID"].Value.ToString());
            goods.Name = dgvGoods.Rows[rowIndex].Cells["NAME"].Value.ToString();
            goods.Count =Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["COUNT"].Value);
            goods.PurchasePrice = Convert.ToSingle(dgvGoods.Rows[rowIndex].Cells["PURCHASEPRICE"].Value);
            goods.SalePrice = Convert.ToSingle(dgvGoods.Rows[rowIndex].Cells["SALEPRICE"].Value);
            goods.WaringCount = Convert.ToInt32(dgvGoods.Rows[rowIndex].Cells["WARINGCOUNT"].Value);
            goods.Description = dgvGoods.Rows[rowIndex].Cells["DESCRIPTION"].Value.ToString();
            return goods;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int nSelectRow = CheckChoose();
            if (nSelectRow < 0)
            {
                return;
            }
            PurchaseForm saleForm = new PurchaseForm(TradeType.Sell);
            saleForm.GoodInfo = buildGoods(nSelectRow);
            saleForm.ShowDialog(this);
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void dgvGoods_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }
    }
}
